#!/usr/bin/env python3.7.9
'''
Copyright © 2021 DUE TUL
@ date  : Monday january 12, 2020
@ desc  : This modules is used to define the data read function
@ author:  
'''
import librosa
import tensorflow as tf
import numpy as np


def _parse_data_function(example):
    data_feature_description = {
        'data': tf.io.FixedLenFeature([128 * 214], tf.float32),
        'label': tf.io.FixedLenFeature([], tf.int64),
    }
    return tf.io.parse_single_example(example, data_feature_description)


def train_reader_tfrecord(data_path, num_epochs, batch_Size=32):
    raw_dataset = tf.data.TFRecordDataset(data_path)
    train_dataset = raw_dataset.map(_parse_data_function)
    train_dataset = train_dataset.shuffle(buffer_size=1000) \
        .repeat(count=num_epochs) \
        .batch(batch_size=batch_Size) \
        .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
    return train_dataset


def test_reader_tfrecord(data_path):
    raw_dataset = tf.data.TFRecordDataset(data_path)
    test_dataset = raw_dataset.map(_parse_data_function)
    test_dataset = test_dataset.batch(batch_size=32)
    return test_dataset


# def train_reader_path(data_list_path, num_epochs):
#     train_sounds, train_labels = [], []
#     with open(data_list_path, 'r') as f:
#         data = f.readlines()
#     for d in data:
#         path, label = d.replace('\n', '').split('\t')
#         y1, sr1 = librosa.load(path, duration=2.97)
#         ps = librosa.feature.melspectrogram(y=y1, sr=sr1, n_mels=128)
#         train_sounds.append(ps[..., np.newaxis])
#         train_labels.append(int(label))
#     train_dataset = tf.data.Dataset.from_tensor_slices((train_sounds, train_labels))
#     train_dataset = train_dataset.shuffle(buffer_size=1000) \
#         .repeat(count=num_epochs) \
#         .batch(batch_size=32) \
#         .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
#     return train_dataset
#
#
# def test_reader_path(data_list_path):
#     test_sounds, test_labels = [], []
#     with open(data_list_path, 'r') as f:
#         data = f.readlines()
#     for d in data:
#         path, label = d.replace('\n', '').split('\t')
#         y1, sr1 = librosa.load(path, duration=2.97)
#         ps = librosa.feature.melspectrogram(y=y1, sr=sr1, n_mels=128)
#         test_sounds.append(ps[..., np.newaxis])
#         test_labels.append(int(label))
#     test_dataset = tf.data.Dataset.from_tensor_slices((test_sounds, test_labels))
#     test_dataset = test_dataset.batch(batch_size=32)
#     return test_dataset

if __name__ == '__main__':
    a = 1
    pass
